package threads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import data.CustomerInfo;
import data.ItemList;
import rm2.DRSserverinit;
import rm2.UDPDatagram;
import exception.CustomerNotExistsException;
import exception.ItemNotExistsException;

public class CheckCustomerInfo {

	String boughtitem;
	CustomerInfo cl;
	String customer;
	int boughtNumber;
	String desireditem;
	int desiredNumber;
	CheckInfo ci;
	DRSserverinit drSserverinit;
	
	public CheckCustomerInfo(String customer_id, String bought_item_id, int bought_number, String desired_item_id, int desired_number,
			DRSserverinit drSserverinit) {
		this.customer = customer_id;
		this.boughtitem = bought_item_id;
		this.boughtNumber = bought_number;
		this.desireditem = desired_item_id;
		this.desiredNumber = desired_number;
		this.drSserverinit=drSserverinit;
	}
	
	public void exchange(){
		try
		{
		String message = "unsuccessful";
		
		//checking bought item on closest server
		if(drSserverinit.slist.get(boughtitem) == null)
		{
			String reply; 
			reply = UDPDatagram.messageSendAndReceive("exchange~" + this.customer + "~" + boughtitem + "~" + this.boughtNumber,
					this.drSserverinit.closest_1_server_udp_port, this.drSserverinit.closest_1_server_address);
			
			if(reply.contains("unsuccessful"))
			{
				reply = UDPDatagram.messageSendAndReceive("buy~" + this.customer + "~" + boughtitem + "~" + this.boughtNumber,
						this.drSserverinit.closest_2_server_udp_port, this.drSserverinit.closest_2_server_address);
			}
			
			if(reply.contains("unsuccessful"))
				throw new ItemNotExistsException(this.customer + " --> Buying" + ": " + "Item: " + boughtitem + " does not exist.");
			else
				message = "successful";
		}
		
		//checking desired item on closest server
		if(drSserverinit.slist.get(desireditem) == null)
		{
			String reply; 
			reply = UDPDatagram.messageSendAndReceive("exchange~" + this.customer + "~" + desireditem + "~" + this.desiredNumber
					+ "~" + desireditem + "~" + this.desiredNumber,
					this.drSserverinit.closest_1_server_udp_port, this.drSserverinit.closest_1_server_address);
			
			if(reply.contains("unsuccessful"))
			{
				reply = UDPDatagram.messageSendAndReceive("exchange~" + this.customer + "~" + desireditem + "~" + this.desiredNumber
						+ "~" + desireditem + "~" + this.desiredNumber,
						this.drSserverinit.closest_2_server_udp_port, this.drSserverinit.closest_2_server_address);
			}
			
			if(reply.contains("unsuccessful"))
				throw new ItemNotExistsException(this.customer + " --> Exchange" + ": " + "Item: " + desireditem + " does not exist.");
			else
				message = "successful";
		}
		
		ci = new CheckInfo(this.drSserverinit);
		if(ci.CheckBoughtItem(customer, boughtitem, boughtNumber ))
		{
			ItemDeduction ca = new ItemDeduction(this.drSserverinit);
			//System.out.println("test3");
			synchronized (drSserverinit.slist) 
			{
				ca.removeItem(customer, boughtitem, boughtNumber);
				System.out.println(customer  +" returned " + boughtNumber + " unit(s) of " + boughtitem);
				message = "return successful";
			}
		}
		
		
		if(ci.CheckDesiredItem(customer, desireditem, desiredNumber))
		{
			ItemAddition ca = new ItemAddition(this.drSserverinit);					
			
			synchronized (drSserverinit.slist.get(desireditem)) 
			{
				ca.addItem(customer, desireditem, desiredNumber);
				message = "bought successful";
			}				
		}
	
		

	}
	catch(Exception e)
	{
		System.out.println(e.getMessage());
	}
	
	}

}
